package com.kch.web.controller.system;


import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.common.utils.DateUtils;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.AuditRecord;
import com.kch.system.domain.SysMemberVoice;
import com.kch.system.service.AuditRecordService;
import com.kch.system.service.ISysMemberVoiceService;
import com.kch.system.vo.AuditRecordVO;
import com.kch.system.vo.SysMemberVoiceVO;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/voice")
@Api(tags = "会员服务")
public class SysMemberVoiceController  extends BaseController {
    private String prefix = "system/voice";

    @Autowired
    private ISysMemberVoiceService sysMemberVoiceService;

    @Autowired
    private AuditRecordService auditRecordService;

    @RequiresPermissions("system:voice:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        return prefix + "/voice";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMemberVoiceVO sysMemberVoice) {
        sysMemberVoice.setUserId(ShiroUtils.getUserId().intValue());
        startPage();
        List<SysMemberVoiceVO> list = sysMemberVoiceService.selectMemberVoicePage(sysMemberVoice);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @Log(title = "会员之声", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated SysMemberVoiceVO memberVoice) {
        SysMemberVoice sysMemberVoice = new SysMemberVoice();
        BeanUtils.copyProperties(memberVoice, sysMemberVoice);
        sysMemberVoice.setCreateBy(ShiroUtils.getLoginName());
        sysMemberVoice.setCreateTime(new Date());
        sysMemberVoice.setUserId(ShiroUtils.getUserId().intValue());
        sysMemberVoice.setCreateBy(ShiroUtils.getLoginName());
        sysMemberVoice.setStatus("0");
        sysMemberVoice.setReviewStatus("0");
        sysMemberVoice.setImg(memberVoice.getImageurl());
        sysMemberVoiceService.insetSysMemberVoice(sysMemberVoice);

        AuditRecord auditRecord = new AuditRecord();
        auditRecord.setBusId(sysMemberVoice.getId());
        auditRecord.setBusType("4");
        auditRecord.setReviewStatus("0");
        auditRecord.setOperTime(new Date());
        auditRecord.setOperUser(ShiroUtils.getUserId().intValue());
        auditRecord.setOpinion("待审核");
        auditRecord.setCreateBy(ShiroUtils.getLoginName());
        auditRecord.setCreateTime(new Date());
        auditRecordService.insertAuditRecord(auditRecord);
        return toAjax(true);
    }

    @Log(title = "会员之声", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(sysMemberVoiceService.deleteMemberVoice(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("voice", sysMemberVoiceService.selectById(id));
        return prefix + "/edit";
    }

    @Log(title = "会员之声", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysMemberVoiceVO voice) {
        try {
            voice.setUpdateBy(ShiroUtils.getLoginName());
            voice.setCreateTime(new Date());
            voice.setUserId(ShiroUtils.getUserId().intValue());
            return toAjax(sysMemberVoiceService.updateById(voice));
        }catch (Exception e){
            return toAjax(false);
        }
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("voice", sysMemberVoiceService.selectById(id));
        return prefix + "/detail";
    }


    @GetMapping("/index")
    public String auditIndex(ModelMap mmap) {
        return prefix + "/audit/voice";
    }

    @PostMapping("/audit/list")
    @ResponseBody
    public TableDataInfo auditList(SysMemberVoiceVO sysMemberVoice) {
        startPage();
        List<SysMemberVoiceVO> list = sysMemberVoiceService.selectMemberVoicePage(sysMemberVoice);
        return getDataTable(list);
    }

    @GetMapping("/audit/{id}")
    public String audit(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        mmap.put("id", id);
        return prefix + "/audit";
    }

    @PostMapping("/audit")
    @ResponseBody
    public TableDataInfo auditList(@RequestParam("id") Integer id) throws Exception {
        if(ObjectUtils.isEmpty(id)){
            List<AuditRecordVO> auditRecords = new ArrayList<>();
            return getDataTable(auditRecords);
        }
        startPage();
        List<AuditRecordVO> auditRecords = auditRecordService.selectByBusId(id, "4");
        return getDataTable(auditRecords);
    }

    @GetMapping("/audit/detail/{id}")
    public String auditEdit(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("voice", sysMemberVoiceService.selectById(id));
        return prefix + "/audit/detail";
    }

    @GetMapping("/pass/{id}")
    public String pass(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        mmap.put("id", id);
        mmap.put("userName", ShiroUtils.getLoginName());
        mmap.put("date", DateUtils.parseDateToStr("yyyy-MM-dd", new Date()));
        mmap.put("status", "审核通过");
        return prefix + "/pass";
    }

    @Log(title = "审核通过", businessType = BusinessType.INSERT)
    @PostMapping("/pass")
    @ResponseBody
    public AjaxResult pass(AuditRecordVO auditRecordVO){
        AuditRecord auditRecord = new AuditRecord();
        auditRecord.setBusId(auditRecordVO.getId());
        auditRecord.setBusType("4");
        auditRecord.setReviewStatus("1");
        auditRecord.setOperTime(new Date());
        auditRecord.setOperUser(ShiroUtils.getUserId().intValue());
        auditRecord.setOpinion(auditRecordVO.getOpinion());
        auditRecord.setCreateBy(ShiroUtils.getLoginName());
        auditRecord.setCreateTime(new Date());
        auditRecordService.insertAuditRecord(auditRecord);

        SysMemberVoice sysMemberVoice = sysMemberVoiceService.selectSysMemberById(auditRecordVO.getId());
        sysMemberVoice.setReviewStatus("1");
        sysMemberVoice.setStatus("1");
        sysMemberVoiceService.updateSysMemberById(sysMemberVoice);
        return AjaxResult.success();
    }

    @GetMapping("/fail/{id}")
    public String fail(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        mmap.put("id", id);
        mmap.put("userName", ShiroUtils.getLoginName());
        mmap.put("date", DateUtils.parseDateToStr("yyyy-MM-dd", new Date()));
        mmap.put("status", "审核不通过");
        return prefix + "/fail";
    }

    @Log(title = "审核不通过", businessType = BusinessType.INSERT)
    @PostMapping("/fail")
    @ResponseBody
    public AjaxResult fail(AuditRecordVO auditRecordVO){
        AuditRecord auditRecord = new AuditRecord();
        auditRecord.setBusId(auditRecordVO.getId());
        auditRecord.setBusType("4");
        auditRecord.setReviewStatus("2");
        auditRecord.setOperTime(new Date());
        auditRecord.setOperUser(ShiroUtils.getUserId().intValue());
        auditRecord.setOpinion(auditRecordVO.getOpinion());
        auditRecord.setCreateBy(ShiroUtils.getLoginName());
        auditRecord.setCreateTime(new Date());
        auditRecordService.insertAuditRecord(auditRecord);

        SysMemberVoice sysMemberVoice = sysMemberVoiceService.selectSysMemberById(auditRecordVO.getId());
        sysMemberVoice.setReviewStatus("2");
        sysMemberVoiceService.updateSysMemberById(sysMemberVoice);
        return AjaxResult.success();
    }

    @GetMapping("/manage")
    public String manage(ModelMap mmap) throws Exception {
        return prefix + "/manage/voice";
    }

    @PostMapping("/manage/list")
    @ResponseBody
    public TableDataInfo manageList(SysMemberVoiceVO pageVO) {
        pageVO.setReviewStatus("1");
        startPage();
        List<SysMemberVoiceVO> list = sysMemberVoiceService.selectMemberVoicePage(pageVO);
        return getDataTable(list);
    }

    @GetMapping("/status")
    @ResponseBody
    public AjaxResult status(Integer id) {
        try {
            sysMemberVoiceService.updateStatus(id);
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
